package jupitermouse.site.source.custom.mysql.test;

import jupitermouse.site.source.custom.mysql.MySqlDataSourceFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.util.stream.Stream;

public class MysqlSourceApp {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource source = env.addSource(new MySqlDataSourceFunction());

        //转换
        source.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Long>> out) throws Exception {
                String[] values = value.toLowerCase().split("\\.");
                Stream.of(values)
                        .forEach(
                                i -> {
                                    out.collect(new Tuple2<>(i, 1L));
                                }
                        );
            }
        }).keyBy(0)
                .timeWindow(Time.seconds(5))
                .sum(1)
                .print()
                .setParallelism(1);

        env.execute("MysqlSourceApp");

    }
}
